//package com.quanxi.qxaimain.service.mongoDb;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.mongodb.client.result.UpdateResult;
//import com.quanxi.qxaimain.common.globalException.GlobalException;
//import com.quanxi.qxaimain.common.mongoDb.entity.IndicatorsDetail;
//import com.quanxi.qxaimain.common.util.sqlFormat.sqlFormat;
//import com.quanxi.qxaimain.service.indicatorsService.indicatorsService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.data.mongodb.core.query.Query;
//import org.springframework.data.mongodb.core.query.Update;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.LinkedHashMap;
//import java.util.List;
//import java.util.Map;
//
////author: Liang Zhang
////date: 2022/10/08
//
//@Slf4j
//@Service
//public class MongoDbService {
//    @Resource
//    private   indicatorsService indicatorsService;
//    @Resource
//    com.quanxi.qxaimain.dao.indicators.indicatorsDataMapper indicatorsDataMapper;
//
//    @Autowired
//    private MongoTemplate mongoTemplate;
//
//    @Async
//    public void test() throws GlobalException, InterruptedException {
//        Thread.sleep(10000);
//
//        JSONArray jsonArr = new JSONArray();
//        jsonArr.add("c8");
//        List<String> indIdList= JSONObject.parseArray(jsonArr.toString(),String.class);
//        String table="qxzsk_t_indicator_sql";
//        List<Map<String, Object>> indicatorsLists = indicatorsService.indicatorsLists(table, indIdList);
//        String sql_detail= indicatorsLists.get(0).get("sql_detail").toString();
////        sql_detail="SELECT DISTINCT T1.OUTPATIENT_DEPT_CN AS 科室,T1.CHARGE_DOCTOR_ID AS 医生编号,T1.CHARGE_DOCTOR_NAME AS 医生,T1.Name AS 患者,T1.Patient_Id AS 病案号,T1.Inpatient_Form_No AS 住院号,to_char(T1.Admission_Date,'yyyy-mm-dd hh24:mi:ss') 入院时间,to_char(T1.Discharge_Date,'yyyy-mm-dd hh24:mi:ss') 出院时间,T2.DIAGNOSIS_NAME 主要诊断,T2.DIAGNOSIS_CODE 疾病编码,T1.ACTUAL_IN_DAYS 住院天数  \n" +
////                "FROM QX_T_MEDICAL_RECORD_PAGE T1 LEFT JOIN QX_T_MEDICAL_RECORD_DIAG T2 ON T1.Inpatient_Form_No = T2.Inpatient_Form_No\n" +
////                "WHERE T1.is_del  = 0 AND T2.DIAG_TYPE_CODE ='1' AND T1.DISCHARGE_DATE BETWEEN to_date({D1},'yyyy-mm-dd hh24:mi:ss')AND to_date({D2},'YYYY-MM-DD hh24:MI:SS')";
//        String D1="2021-02-01 00:00:00";
//        String D2="2021-02-28 00:00:00";
//        String dept_code = "";;
//
//        String  sqlformat= sqlFormat.sqlFormat(sql_detail,D1,D2,dept_code);
//
//        List<LinkedHashMap<String, Object>> linkedHashMaps = indicatorsDataMapper.exec_sql(sqlformat);
//        String ind_id = "c8";
//
//        String s = JSON.toJSONString(linkedHashMaps, SerializerFeature.WriteMapNullValue);
//        addToMongoDB(ind_id, D1, D2, s);
//        // System.out.println(linkedHashMaps);
//
//        findFromMongoDB(ind_id, D1, D2);
//    }
//
//    @Async
//    public void addToMongoDB(String ind_id, String start_date, String end_date, String result){
////        if (!findFromMongoDB(ind_id, start_date, end_date)) {
////            IndicatorsDetail data = new IndicatorsDetail()
////                    .setInd_id(ind_id)
////                    .setStart_date(start_date)
////                    .setEnd_date(end_date)
////                    .setResult(result);
////            indicatorsDetailProcess.save(data);
////        }
////        else {
//        Criteria condition = Criteria.where("ind_id").is(ind_id).and("start_date").is(start_date).and("end_date").is(end_date);
//        Query query = new Query(condition);
//        Update update = new Update();
//        update.set("result", result);
//        UpdateResult upsert = mongoTemplate.upsert(query, update, IndicatorsDetail.class);
//        log.info(upsert.toString());
////       }
//    }
//
//    @Async
//    public boolean findFromMongoDB(String ind_id, String start_date, String end_date){
//
////        Criteria condition = Criteria.where("ind_id").is(ind_id).and("discharge_date").gte(start_date)
////                .lte(end_date);
//        Criteria condition = Criteria.where("ind_id").is(ind_id).and("start_date").is(start_date).and("end_date").is(end_date);
//        Query query = new Query(condition);
//        // List<IndicatorsDetail> personList = indicatorsDetailProcess.findAll();
//        List<IndicatorsDetail> personList = mongoTemplate.find(query, IndicatorsDetail.class);
//        log.info(personList.toString());
//        return personList == null;
//
//    }
//}
//
